package com.bos.controller;


import com.bos.data.model.BosParamModel;
import com.bos.service.Bos_paramService;
import com.bos.service.LogsService;
import com.bos.common.PageData;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class Bos_paramController {
    @Autowired
    @Qualifier("bos_paramServiceImpl")
    Bos_paramService paramService;
    @Autowired
    private LogsService logsService;

    //1.查询出Bos_param中的数据
    @RequestMapping(value = "/findParams")
    public String findParam(Model model, HttpServletRequest request) {
        //给定默认的页数和页的大小
        int pageNo = 1, pageSize = PageData.DEFAULTPAGESIZE;
        String pageIndex = request.getParameter("pageNo");
        String setPageSize = request.getParameter("setPageSize");
        if (!Strings.isNullOrEmpty(pageIndex)) {
            pageNo = Integer.valueOf(pageIndex);
        }
        if (!Strings.isNullOrEmpty(setPageSize)) {
            pageSize = Integer.valueOf(setPageSize);
        }
        String modelName = request.getParameter("modelName");
        String modelAccount = request.getParameter("modelAccount");
        PageData params = paramService.findParam(pageSize, pageNo, modelName, modelAccount);
        model.addAttribute("list", params);
        model.addAttribute("modelName", modelName);
        model.addAttribute("modelAccount", modelAccount);
        return "setting";
    }

    //2,能够实现添加模块信息的功能
    @RequestMapping(value = "/addModule")
    public String insertModule(@RequestParam("typeName") String typeName, @RequestParam("paramValue") String paramValue, HttpSession session) {
        //根据paramId来排序，查询出paramId,和typeId的值
        BosParamModel params2 = paramService.findParamId();
        String paramId = params2.getParamId();
        int num = Integer.parseInt(paramId) + 1;//将该数字加一
        String added = String.valueOf(num);
        //那么我要将模块名称和模块描述插入到数据库表中
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String updateTime = sdf.format(new Date());
        BosParamModel param = new BosParamModel(params2.getTypeId(), typeName, added, paramValue, updateTime);
        int row = paramService.insertParams(param);
        //插入日志
        logsService.insertLogs(session, "添加字典数据");
        return "redirect:findParams";
    }

    //3.根据id删除数据
    @RequestMapping(value = "/deleteById")
    public String deleteByID(@RequestParam("id") int id, HttpSession session) {
        //调用数据删除的方法
        int row = paramService.deleteById(id);
        //插入日志
        logsService.insertLogs(session, "删除字典数据");
        return "redirect:findParams";
    }
    //4.根据id修改模块描述的数据
    @RequestMapping(value = "/updateByParamValue")
    public String updateByParam(@RequestParam("cid") int cid,@RequestParam(value = "paramValue") String paramValue){
        // System.out.println(paramValue);
        System.out.println(paramValue.trim());
        BosParamModel param=new BosParamModel();
        param.setId(cid);
        if(!paramValue.equals("")){
            param.setParamValue(paramValue.trim());
        }else{
            param.setParamValue("");
        }
        int row=paramService.updateByParam(param);
        return "redirect:findParams";
    }
}
